#include "InputBox.h"
#include <iostream>

#if INSTALANDO
#define PATH_RES PATH_RESOURCES
#else
#define PATH_RES "../Resources/"
#endif

InputBox::InputBox( int posx, int posy, int w, int h, unsigned int p, unsigned int code, string contenido ) {
	texto = new StringInput(contenido);
	x = posx;
	y = posy;
	ancho = w;
	alto = h;
	on = 0;
	maxC = w/11;
	paso = p;
	id = code;
	string path_res = PATH_RES;
	font14 = TTF_OpenFont((path_res+"fontArial.ttf").c_str(), 14 );            //OJO! HARDCODE
}

int InputBox::getX() {
	return x;
}

int InputBox::getY() {
	return y;
}

int InputBox::getAlto() {
	return alto;
}

int InputBox::getAncho() {
	return ancho;
}

StringInput* InputBox::getTexto() {
	return texto;
}

int InputBox::getOn(){
	return on;
}

int InputBox::getMaxC() {
	return maxC;
}

unsigned int InputBox::getId() {
	return id;
}

unsigned int InputBox::getPaso(){
	return paso;
}

TTF_Font* InputBox::getFont14() {
	return font14;
}

void InputBox::setTexto(StringInput* valor) {
	texto = valor;
}

void InputBox::setOn(int i) {
	on = i;
}

void InputBox::dibujarInput(SDL_Surface* superficie) {

	SDL_Surface* borde;
	SDL_Surface* box;

	borde = SDL_CreateRGBSurface(0, getAncho(), getAlto(), 32, 255, 255, 255, 0 );
	if (getOn() == 0) {
		SDL_FillRect(borde, NULL, SDL_MapRGB(borde->format, 0, 0, 0));
	} else {
		SDL_FillRect(borde, NULL, SDL_MapRGB(borde->format, 170, 170, 170));
	}

	box = SDL_CreateRGBSurface(0, getAncho() - 2, getAlto() - 2, 32, 255, 255, 255, 0 );
	SDL_FillRect(box, NULL, SDL_MapRGB(box->format, 255, 255, 255));

	if ( (getOn() == 0)  && (getTexto()->getString() == "") ) {
		SDL_Surface* msj_escribir;
		SDL_Color color = { 204, 204, 204 };

		msj_escribir = TTF_RenderText_Solid( getFont14(), "<Ingresar>", color );
		insertar_imagen(2, 1, msj_escribir, box);          //OJO! HARDCODE

		SDL_FreeSurface( msj_escribir );

	} else {
		SDL_Surface* msj_escribir;
		SDL_Color color = { 0, 0, 0 };
		
		//Manejar sufijo
		string display =  getTexto()->getString();
		if ( 10 *  getTexto()->getString().size() > getAncho() ) {		
			int cant_c = getAncho() / 10;
			cout << getTexto()->getString().size() - cant_c << flush;
			display = getTexto()->getString().substr (getTexto()->getString().size() - cant_c); 
		}
		
		
		
		msj_escribir = TTF_RenderText_Solid( getFont14(), display.c_str() , color );
		insertar_imagen(2, 1, msj_escribir, box);
		SDL_FreeSurface( msj_escribir );
	}

	insertar_imagen(1, 1, box, borde);
	insertar_imagen(getX(), getY(), borde, superficie);

	SDL_FreeSurface( borde );
	SDL_FreeSurface( box );
}

void InputBox::handleInput(SDL_Event event, int s_x, int s_y) {

    if ( event.type == SDL_MOUSEBUTTONDOWN )
    {

    	if( event.button.button == SDL_BUTTON_LEFT )
    	{
    		int x = event.button.x;
    		int y = event.button.y;

    		if( ( x > getX() + s_x ) &&
    		    ( x < getX() + s_x + getAncho() ) &&
    			( y > getY() + s_y ) &&
    			( y < getY() + s_y + getAlto() ) ) {

                		setOn(1);

    		} else {

    			        setOn(0);

    		}

    	}
    }

    if ( getOn() == 1 ) {
    	getTexto()->handle_input( event, getFont14(), getId() );
    }

}

